M8: native conv via mlx::core::conv_general - #17
Merged
Merged
Conversation
Lift Backend.conv/4 from the BinaryBackend fallback onto Native.conv_general (already bound to mlx::core::conv_general since M1). The fallback was correct but CPU-bound — ≥90% of ViT and Whisper forward-pass cost. Layout translation: MLX conv_general expects NHWC input and OHWI weight; Nx delivers tensors in the caller's layout plus input/kernel/output_permutation opts pointing at canonical NCHW/OIHW. Compose the caller's permutations with the NCHW↔NHWC and OIHW↔OHWI transposes, applying the inverse of output_permutation on the way out (Nx delivers it in user→canonical form; see deps/nx/lib/nx/shape.ex:729-735). Two ordered transposes rather than one composed — MLX lazy-fuses them and the step-wise form is obviously correct across rank 3, 4, and 5. Integer inputs get cast to out.type via Native.astype before the transpose chain (Nx.conv returns float but does not cast operands; MLX conv is float-only). Same-type astype is elided by MLX. batch_group_size > 1 and complex-typed conv still route through via_binary — no MLX primitive, and neither appears in the pinned Bumblebee ref. Whisper full-checkpoint conformance: 403.8s → 1.8s (~224× speedup). ViT full-checkpoint conformance: similar scale (1.9s end-to-end). New oracle suite in test/emily/backend_conv_test.exs covers 1-D/2-D/ 3-D, stride/:same/:valid/asymmetric padding, kernel and input dilation, grouped and depthwise conv, all three permutation options (independently and combined for NHWC end-to-end), and integer-input coercion. test/emily/backend_fallbacks_test.exs: dropped the obsolete "conv routes through BinaryBackend" test; added a batch_group_size > 1 case asserting the rare fallback still matches BinaryBackend.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Backend.conv/4from thevia_binaryfallback ontoNative.conv_general(already bound tomlx::core::conv_generalsince M1). The fallback was correct but CPU-bound — ≥90% of ViT and Whisper forward-pass cost.input_permutation,kernel_permutation, andoutput_permutationopts with the canonical-layout transposes. Apply the inverse ofoutput_permutationon the way out (Nx delivers it in user→canonical form; seedeps/nx/lib/nx/shape.ex:729-735).out.typeviaNative.astypebefore dispatching —Nx.convreturns float but does not cast inputs, and MLX conv is float-only. Same-type astype is elided by MLX.batch_group_size > 1and complex-typed conv still route throughvia_binary— no MLX primitive, and neither appears in the pinned Bumblebee ref.Speedups
mix test --only whisper_fullmix test --only vit_fullTest plan
mix precommit— compile warnings-as-errors, format, credo --strict, 198 tests + 56 properties + 1 doctest greenmix test --only conformance— 16/16 tiny-random (DistilBERT, Qwen3, ViT, Whisper)mix test --only vit_full— ViT full-checkpointmix test --only whisper_full— Whisper full-checkpointtest/emily/backend_conv_test.exsoracle suite vsNx.BinaryBackend::same/:valid/ explicit asymmetric paddingkernel_dilationandinput_dilation > 1test/emily/backend_fallbacks_test.exs— removed obsolete "conv routes through BinaryBackend" test, addedbatch_group_size > 1fallback coverage